﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using Telerik.Web.UI;
using System.Data;

namespace AllNations.WebAppAdmin
{
    public partial class SmallgroupManager : MakeItWebAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gridSmallgroup.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler((obj, args) => RefreshData(false));
            gridSmallgroup.InsertCommand += new Telerik.Web.UI.GridCommandEventHandler(gridSmallgroup_InsertCommand);
            gridSmallgroup.UpdateCommand += new Telerik.Web.UI.GridCommandEventHandler(gridSmallgroup_UpdateCommand);
            if (!IsPostBack)
            {
                RefreshData(true);
            }
        }

        void gridSmallgroup_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;

            Guid iSmallGroupId = (Guid)editedItem.GetDataKeyValue("smallgroup_id");

            SaveSmallGroupDetail(iSmallGroupId, editedItem);
        }

        void gridSmallgroup_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;

            SaveSmallGroupDetail(Guid.Empty, editedItem);
        }

        private void SaveSmallGroupDetail(Guid guidSmallGroupId, GridEditableItem editedItem)
        {
            // this is to get schema
            RefreshData(false);

            var ds = gridSmallgroup.DataSource as DataSet;
            var drNew = ds.Tables[0].NewRow();
            editedItem.UpdateValues(drNew);

            StoredProcArgs args = new StoredProcArgs();
            if (Guid.Empty != guidSmallGroupId)
                args["pSmallgroupId"] = guidSmallGroupId;
            if (!drNew.IsNull("group_nm"))
                args["pGroupNm"] = drNew["group_nm"];
            if (!drNew.IsNull("group_leader_id"))
                args["pGroupLeaderId"] = drNew["group_leader_id"];
            if (!drNew.IsNull("group_leader_nm"))
                args["pGroupLeaderNm"] = drNew["group_leader_nm"];
            if (!drNew.IsNull("group_leader_email"))
                args["pGroupLeaderEmail"] = drNew["group_leader_email"];
            if (!drNew.IsNull("group_leader_phone"))
                args["pGroupLeaderPhone"] = drNew["group_leader_phone"];
            if (!drNew.IsNull("description_txt"))
                args["pDescriptionTxt"] = drNew["description_txt"];

            GenericCud.ExecuteCud("proc_cm_SaveSmallGroup", args);

            RefreshData(true);
        }

        private void RefreshData(bool bDataBind)
        {
            gridSmallgroup.DataSource = GenericQry.GetDataSet("proc_cm_ListSmallGroup", new StoredProcArgs(), !bDataBind);
            if (bDataBind)
                gridSmallgroup.DataBind();
        }

        protected void lnkDeleteItem_Click(object sender, EventArgs e)
        {
            Guid guid = new Guid(((LinkButton)sender).CommandArgument);
            StoredProcArgs args = new StoredProcArgs();
            args["pSmallGroupId"] = guid;
            GenericCud.ExecuteCud("proc_cm_DeleteSmallGroup", args);

            RefreshData(true);
        }

        protected void lnkDeleteMember_Click(object sender, EventArgs e)
        {
            Guid guid = new Guid(hidSelectedSmallgroupId.Value);
            int memberId = int.Parse(((LinkButton)sender).CommandArgument);

            StoredProcArgs args = new StoredProcArgs();
            args["pSmallGroupId"] = guid;
            args["pMemberId"] = memberId;
            GenericCud.ExecuteCud("proc_cm_DeleteSmallGroupMember", args);

            RefreshSmallGroupMember(guid);
            RefreshData(true);
        }

        protected void lnkEditMember_Click(object sender, EventArgs e)
        {
            Guid guid = new Guid(((LinkButton)sender).CommandArgument);

            hidSelectedSmallgroupId.Value = guid.ToString();
            RefreshSmallGroupMember(guid);

            SmallGroupViews.SelectedIndex = EditMemberView.Index;
        }

        private void RefreshSmallGroupMember(Guid guid)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pSmallGroupId"] = guid;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetSmallGroup", args))
            {
                DataRow dr = ds.Tables[0].Rows[0];
                lblSmallGroupName.Text = dr["group_nm"].ToString();
                gridSmallgroupMember.DataSource = ds.Tables[1];
                gridSmallgroupMember.DataBind();

                ddAvailableMembers.DataSource = ds.Tables[2];
                ddAvailableMembers.DataBind();
            }
        }

        protected void btAddMembers_Click(object sender, EventArgs e)
        {
            // save the members
            Guid guid = new Guid(hidSelectedSmallgroupId.Value);
            int memberId = int.Parse(ddAvailableMembers.SelectedValue);
            StoredProcArgs args = new StoredProcArgs();
            args["pSmallGroupId"] = guid;
            args["pMemberId"] = memberId;
            GenericCud.ExecuteCud("proc_cm_AddSmallGroupMember", args);

            RefreshSmallGroupMember(guid);
            RefreshData(true);
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            SmallGroupViews.SelectedIndex = Listview.Index;
        }
    }
}